VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ClsGrupoVentaAutomatica"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

'Clase de GrupoVentaAutomatica

Public WithEvents orset As ClsRecordset
Attribute orset.VB_VarHelpID = -1

Public Event DataChanged()

Public codigo As String
Public codTipoServicio As String
Public codTipoCliente As String
Public descripcion As String
Public detalle_necesario As Boolean
Public detalle_inicial As Integer
Public nro_generaciones As Integer
Public dflt_periodo_facturacion As Integer
Public dflt_maxven_np As Integer

'Constructor y Destructor
Private Sub Class_Initialize()
    Set orset = New ClsRecordset
    orset.setCampos dbCampos(grv)
    orset.setTablas Array(grv)
End Sub
Private Sub Class_Terminate()
    Set orset = Nothing
End Sub
' Generic Data management methods
Public Sub consulta()
    orset.consulta
End Sub
Public Sub consultaDatos(codig As String)
    orset.consulta Array(grv_id & "=" & codig)
    orset.CierraRec
End Sub
Public Sub consultaXTipoServicio(codTse As String)
    orset.consulta Array(grv_tse_id & "=" & codTse)
    orset.CierraRec
End Sub
Public Function buscaServicioSiguiente()
    'busca el servicio que tiene detalles a generar
    Dim sq1 As ClsSQL
    Set sq1 = New ClsSQL
    Dim sq2 As ClsSQL
    Set sq2 = New ClsSQL
    sq2.setCampos Array("count(" & gva_id & ")")
    sq2.setTablas Array(ser & " as s", gva)
    sq2.setCondiciones Array("s." & ser_id & "=s2." & ser_id, _
                             gva_ser_id & "=s." & ser_id)
    sq2.setAgrupaPor Array("s." & ser_id)
    
    sq1.setCampos Array("s2." & ser_id, "count(" & dta_id & ")-" & coalesceFuncSQL("(" & sq2.sSelect & ")", "0") & " as total")
    sq1.setTablas Array(dta, ser & " as s2")
    sq1.setCondiciones Array(dta_ser_id & "=s2." & ser_id, _
                             "s2." & ser_tse_id & "=" & codTipoServicio)
    sq1.setAgrupaPor Array(ser_id)
    
    orset.consultaGral Array(ser_id), _
                       Array("(" & sq1.sSelect & ") as sergva"), _
                       Array("total>0"), , False
    If orset.numRegistros > 0 Then
        buscaServicioSiguiente = orset.getDato(ser_id, "")
    Else
        buscaServicioSiguiente = ""
    End If
    orset.CierraRec
End Function
Public Sub actualizaCGC()
    Dim sqtmp1 As ClsSQL
    Set sqtmp1 = New ClsSQL
    Dim sqtmp2 As ClsSQL
    Set sqtmp2 = New ClsSQL
    
    sqtmp1.setCampos Array(cli_id)
    sqtmp1.setTablas Array(cli, grv, tcl, ctc)
        sqtmp2.setCampos Array(cgc_cli_id)
        sqtmp2.setTablas Array(cgc)
        sqtmp2.setCondiciones Array(cgc_grv_id & "=" & Me.codigo)
        sqtmp2.sSelect
    sqtmp1.setCondiciones Array(tcl_id & "=" & grv_tcl_id, _
                                tcl_id & "=" & ctc_tcl_id, _
                                cli_id & "=" & ctc_cli_id, _
                                grv_id & "=" & Me.codigo, _
                                cli_id & " not in (" & sqtmp2.cadena & ")")
    sqtmp1.sSelect
    orset.AbreRec sqtmp1.cadena
    If orset.numRegistros > 0 Then
        'se tiene que actualizar
        Dim caract As ClsCaractGrvCli
        Set caract = New ClsCaractGrvCli
        While orset.CanMoveNext
            caract.Nuevo orset.mrec.Fields(cli_id), Me.codigo, 1, Me.dflt_periodo_facturacion, Me.dflt_maxven_np, 0, Me.dflt_periodo_facturacion, "1"
            orset.MoveNext
        Wend
    End If
    orset.CierraRec
End Sub
Private Sub Reload_members()
    With orset
        codigo = .getDato(grv_id)
        codTipoServicio = .getDato(grv_tse_id)
        codTipoCliente = .getDato(grv_tcl_id)
        descripcion = .getDato(grv_descripcion)
        detalle_necesario = .getDato(grv_detalle_necesario)
        detalle_inicial = .getDato(grv_detalle_inicial)
        nro_generaciones = .getDato(grv_nro_generaciones)
        dflt_periodo_facturacion = .getDato(grv_dflt_periodo_facturacion)
        dflt_maxven_np = .getDato(grv_dflt_maxven_np)
    End With
    RaiseEvent DataChanged
End Sub
Public Sub Nuevo(newtipoServicioCodigo As String, newtipoClienteCodigo As String, newDescripcion As String, newdetalle_necesario As Integer, newdetalle_inicial As Integer, newnro_generaciones As Integer, newdflt_periodo_facturacion As Integer, newdflt_maxven_np As Integer)
    orset.sInsert Array("", newtipoServicioCodigo, newtipoClienteCodigo, newDescripcion, newdetalle_necesario, newdetalle_inicial, newnro_generaciones, newdflt_periodo_facturacion, newdflt_maxven_np)
    'una vez creada la generacion, se generan las caracteristicas para todos los clientes de este tipo
    Dim clientes As ClsCliente
    Set clientes = New ClsCliente
    clientes.consulta newtipoClienteCodigo
    Dim caract As ClsCaractGrvCli
    Set caract = New ClsCaractGrvCli
    While Not clientes.orset.IsEOF
        caract.Nuevo clientes.codigo, Me.codigo, 1, Me.dflt_periodo_facturacion, Me.dflt_maxven_np, "0", Me.dflt_periodo_facturacion, "1"
        clientes.orset.MoveNext
    Wend
End Sub
Public Sub Modificar(codig As String, newtipoServicioCodigo As String, newtipoClienteCodigo As String, newDescripcion As String, newdetalle_necesario As Integer, newdetalle_inicial As Integer, newnro_generaciones As Integer, newdflt_periodo_facturacion As Integer, newdflt_maxven_np As Integer)
    orset.sUpdate Array(codig, newtipoServicioCodigo, newtipoClienteCodigo, newDescripcion, newdetalle_necesario, newdetalle_inicial, newnro_generaciones, newdflt_periodo_facturacion, newdflt_maxven_np)
End Sub
Public Sub Borrar(codig As String)
    orset.sDelete Array(grv_id & "=" & codig)
End Sub
Private Sub orset_DataChanged()
    Reload_members
End Sub
